package com.lu.controller;

import com.lu.pojo.User;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.xml.crypto.Data;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserDemo {

    @RequestMapping("/find1")
    public String findUser(@RequestParam(value = "name",defaultValue = "魏露露",required = false) String name,String password){
        System.out.println(name+password);
        return "/success.jsp";
    }

    @RequestMapping("/find2")
    public String findUser2(User user,@RequestParam(value = "name2",required = false) String name){
        System.out.println(user+name);
        System.out.println("2222");
        return "/success.jsp";
    }

    @RequestMapping("/find3")
    public String findUser3(User user){
        System.out.println(user);
        System.out.println("2222");
        return "/success.jsp";
    }

    @RequestMapping("/find4")
    /*参数为集合类型时,需要使用@Requestparam注解,告诉springmvc这不是对象;否则,springmvc会当成对象处理,创建对象,而List时接口无法创建对象,
    * 若将list换成arraylist虽然可以创建对象,但是对象里面没有对应的属性名,所以无法传值*/
    public String findUser4(@RequestParam("hobby") List<String> hobby){
        System.out.println(hobby);
        System.out.println(hobby.get(0)+"  "+hobby.get(1));
        return "/success.jsp";
    }

    @RequestMapping("/find5")
    public String findUser5(User user){
        System.out.println(user);
        System.out.println(user.getAddress().getName());
        return "/success.jsp";
    }

    @RequestMapping("/find6")
    public String findUser6(User user){
        System.out.println(user);
        System.out.println(user.getList().get(1).getName());
        return "/success.jsp";
    }

    @RequestMapping("/find7")
    public String findUser7(User user){
        System.out.println(user.getDate());
        return "/success.jsp";
    }
    @RequestMapping("/find8")
    /*@DateTimeFormat 日期格式类型转换器*/
    public String findUser8(/*@DateTimeFormat(pattern = "yyyy-MM-dd")*/Date date){
        System.out.println(date);
        return "/success.jsp";
    }

    /*value请求路径,method用于设置请求的方法可设置多个或单个,
    params用于限制请求参数的条件,headers指定限制请求的请求头信息*/
    @RequestMapping(value = "/find9",method = {RequestMethod.POST,RequestMethod.GET} ,
            params = "money!=100",
            headers = {"Accept-Language=zh-CN,zh;q=0.9,en;q=0.8,en-GB;q=0.7,en-US;q=0.6","User-Agent=Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.182 Safari/537.36 Edg/88.0.705.81"})
    public String findUser9(Integer money){
        System.out.println(money);
        return "/success.jsp";
    }

    /*返回前面带/是访问根目录下,不带/是访问类上注解路径下*/
    @RequestMapping(value = "/find10")
    public String findUser10(Integer money){
        System.out.println(money);
        return "success.jsp";
    }

    /*返回引号里面带forward表示转发(默认),redirect是重定向*/
    @RequestMapping(value = "/find11")
    public String findUser11(Integer money){
        System.out.println(money);
        //return "redirect:/success.jsp";
        return "forward:/success.jsp";
    }

    /*视图解析器(指定前后缀)*/
    @RequestMapping(value = "/find12")
    public String findUser12(Integer money){
        System.out.println(money);
        return "success";
    }
}
